# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=simdjson
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.0.1
pkgrel=1
pkgdesc="A C++ library to see how fast we can parse JSON with complete validation (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://simdjson.org/'
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc")
options=('strip' '!debug' 'staticlibs')
source=("${_realname}-${pkgver}.tar.gz::https://github.com/simdjson/simdjson/archive/v${pkgver}.tar.gz")
sha512sums=('614d77123cc100a34b124803092782ac55d9a9e88b5a1b8f068671dcb164e818ed1516964222ef9f6393ca8c504d70b5d3aac266adc85ab335676b110ac3b4bd')

build() {
  [[ -d "${srcdir}/build-dynamic-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-dynamic-${MSYSTEM}"
  mkdir -p "${srcdir}/build-dynamic-${MSYSTEM}" && cd "${srcdir}/build-dynamic-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DBUILD_SHARED_LIBS=ON \
    "../${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
  
  [[ -d "${srcdir}/build-static-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-static-${MSYSTEM}"
  mkdir -p "${srcdir}/build-static-${MSYSTEM}" && cd "${srcdir}/build-static-${MSYSTEM}"
  
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DBUILD_SHARED_LIBS=OFF \
    "../${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

check() {
  cd "${srcdir}/build-dynamic-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/ctest.exe ./ || true

  cd "${srcdir}/build-static-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/ctest.exe ./ || true
}

package() {
  cd "${srcdir}/build-static-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install ./

  cd "${srcdir}/build-dynamic-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install ./

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/README.md" "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/README.md"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
